package swingExtended;

import java.util.ArrayList;
import javax.swing.ListModel;
import javax.swing.event.ListDataEvent;
import javax.swing.event.ListDataListener;

/**
 *
 * @author Shimu
 * @date 12-Jan-2013
 */
public class ArrayListModel<T> implements ListModel {

    private final ArrayList<T> ELEMENTS = new ArrayList<T>();
    
    private final ArrayList<ListDataListener> LISTENER = 
            new ArrayList<ListDataListener>();
    
    public ArrayListModel() {
    }
    
    public void addElement(T element) {        
        this.ELEMENTS.add(element);    
        final int index = this.ELEMENTS.size() - 1;
        for (ListDataListener l : this.LISTENER) {
            l.intervalAdded(new ListDataEvent(this,ListDataEvent.INTERVAL_ADDED,
                    index, index));
        }
    }
    
    @Override
    public int getSize() {
        return this.ELEMENTS.size();
    }

    @Override
    public Object getElementAt(int index) {
        return this.ELEMENTS.get(index);
    }

    @Override
    public void addListDataListener(ListDataListener l) {
        this.LISTENER.add(l);
    }

    @Override
    public void removeListDataListener(ListDataListener l) {
        this.LISTENER.remove(l);
    }
    
}
